class FormatDate {

  /**
   * 时间格式化
   * yyyy-MM-dd HH:mm:ss  年-月-日 时:分:秒
   */
  static formatTime(date, format) {
    date = this.dateType(date);
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()

    month = month < 10 ? ('0' + month) : month;
    day = day < 10 ? ('0' + day) : day;
    hour = hour < 10 ? ('0' + hour) : hour;
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;

    format = format.replace('yyyy', year).replace('MM', month).replace('dd', day).replace('HH', hour).replace('mm', minute).replace('ss', second);

    return format;

    //return [year, month, day].map(this.formatNumber).join('/') + ' ' + [hour, minute, second].map(this.formatNumber).join(':')
  }

  static formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  }

  static dateType(date) {
    console.log('date type=', typeof date);
    if (typeof date === "number") {
      return new Date(date);
    } else if (typeof date === "string") {
      return new Date(date.replace(/-/g, '/'));
    } else if (typeof date === "object") {
      return date;
    }
  }

}


module.exports = FormatDate;